//
//  CellOfNews.m
//  UI12_01_UITableView 自定义cell
//
//  Created by wyx on 16/3/24.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "CellOfNews.h"
#import "ModleOfContact.h"

#pragma mark - 宏
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

#pragma mark - 声明
@interface CellOfNews ()
//子控件声明属性
@property (nonatomic, retain) UILabel *lableOfDate;
@property (nonatomic, retain) UILabel *lableOfSummary;
@property (nonatomic, retain) UILabel *lableOfTitle;
@property (nonatomic, retain) UIImageView *iamgeOfPic;
@end
@implementation CellOfNews
#pragma mark - delloc
-(void)dealloc{
    [_iamgeOfPic release];
    [_lableOfDate release];
    [_lableOfTitle release];
    [_lableOfSummary release];
    [super dealloc];
}
#pragma mark - override init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //创建字控件
        [self createSubView];
        [self layoutSubviews];
    }
    return self;
}
#pragma mark - 创建子控件 View
- (void)createSubView{
    self.iamgeOfPic = [[UIImageView alloc] init];
    [self.contentView addSubview:self.iamgeOfPic];
    //self.iamgeOfPic.backgroundColor = [UIColor yellowColor];
    [_iamgeOfPic release];
    
    self.lableOfTitle = [[UILabel alloc] init];
    [self.contentView addSubview:self.lableOfTitle];
     //self.lableOfTitle.backgroundColor = [UIColor yellowColor];
    [_lableOfTitle release];
    
    self.lableOfDate = [[UILabel alloc] init];
    [self.contentView addSubview: self.lableOfDate];
     //self.lableOfDate.backgroundColor = [UIColor yellowColor];
    self.lableOfDate.textAlignment = 2;
    [_lableOfDate release];
    
    self.lableOfSummary = [[UILabel alloc] init];
    [self.contentView addSubview:self.lableOfSummary];
     //self.lableOfSummary.backgroundColor = [UIColor yellowColor];
    self.lableOfSummary.numberOfLines = 0;
    [_lableOfSummary release];
    
}
#pragma mark - override layoutSubviews
- (void)layoutSubviews{
    [super layoutSubviews];
    //设置子控件frame 属性
    self.iamgeOfPic.frame = CGRectMake(5, 5, 85*4/3.0, 85);
    self.lableOfTitle.frame = CGRectMake(85*4/3.0+15, 5, WIDTH-20-85*4/3.0, 40);
    self.lableOfDate.frame = CGRectMake(WIDTH-185, 50, 150, 40);
    //summary的度自适应
    CGFloat heigh = [CellOfNews heightOfCellNews:self.lableOfSummary.text];
    self.lableOfSummary.frame = CGRectMake(5, 95, WIDTH-10, heigh- 85 - 15 );
    
}
#pragma mark - 子控件赋值
- (void)setCellContentValue:(ModleOfContact *)modle{
    //子控件赋值
    self.iamgeOfPic.image = [UIImage imageNamed:@"1.jpg"];
    //调用时间格式转换方法
    self.lableOfDate.text = [self dateFormatReverseWithTimeIntervale:modle.lastUpdateTime];
    self.lableOfTitle.text = modle.title;
    self.lableOfSummary.text = modle.summary;
    
}
#pragma mark - 时间格式的转换 详见 API: NSDate
- (NSString *)dateFormatReverseWithTimeIntervale:(NSString *)timeInterval {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval.doubleValue];
    //格式转化
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    return formattedDateString;
}
#pragma mark - 计算 新的矩形   Cell 高度
+ (CGFloat)heightOfCellNews:(NSString *)text{
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 10;
    //计算新的矩形
    //参数1:预估大小
    //参数2:绘图选项
    //参数3:文字属性
    //参数4:绘制上下文
    CGRect newRect = [text boundingRectWithSize:CGSizeMake(width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    return newRect.size.height + 85 + 15;
}
#pragma mark - other
- (void)awakeFromNib {
    // Initialization code
    
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
